package com.exam.sacrifice.ui;

import static android.view.View.INVISIBLE;

import android.os.Handler;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.exam.commonbiz.base.BaseFragment;
import com.exam.commonbiz.util.CollectionUtil;
import com.exam.sacrifice.R;
import com.exam.sacrifice.databinding.FragmentActivityBinding;
import com.exam.sacrifice.ui.adapter.ActivityAdapter;
import com.exam.sacrifice.ui.bean.ActivityInfo;
import com.exam.sacrifice.ui.bean.ActivityListRes;
import com.exam.sacrifice.viewmodel.ActivityViewModel;
import com.exam.sacrifice.viewmodel.IActivityCallBack;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

public class ActivityFragment extends BaseFragment<ActivityViewModel, FragmentActivityBinding> implements IActivityCallBack, OnLoadMoreListener {

    private int pageIndex = 0;
    private String keyword;
    private ActivityAdapter activityAdapter;

    public static ActivityFragment newInstance() {
        ActivityFragment fragment = new ActivityFragment();
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_activity;
    }

    @Override
    public void initData() {

        mViewModel.setActivityCallBack(this);
        binding.include.ivback.setVisibility(INVISIBLE);
        binding.include.tvTitle.setText("活动");
        binding.smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshData();
            }
        });

        activityAdapter = new ActivityAdapter();
        LinearLayoutManager gridLayoutManager = new LinearLayoutManager(context);
        binding.recyclerView.setLayoutManager(gridLayoutManager);
        binding.recyclerView.setAdapter(activityAdapter);
        activityAdapter.setOnItemClickListener(new ActivityAdapter.OnItemClickListener() {

            @Override
            public void onItemClick(ActivityInfo item) {
                actionStart(ActivityDetail.class);
            }
        });

        binding.smartRefreshLayout.setEnableLoadMore(false);
        refreshData();
    }

    private void refreshData() {
        pageIndex = 0;
        mViewModel.activityList(pageIndex, keyword);
    }

    private void loadMoreData() {
        ++pageIndex;
        mViewModel.activityList(pageIndex, keyword);
    }

    @Override
    protected void loadData() {

    }

    @Override
    public void onActivityList(ActivityListRes activityListRes) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                binding.smartRefreshLayout.finishRefresh();
            }
        }, 1500);
        binding.smartRefreshLayout.finishLoadMore();
        if (activityListRes == null) {
            return;
        }
        if (pageIndex == 0) {
            activityAdapter.setNewData(activityListRes.records);
        } else {
            activityAdapter.addData(activityListRes.records);
        }
        if (CollectionUtil.isEmpty(activityListRes.records)) {
            binding.smartRefreshLayout.setEnableLoadMore(false);
            binding.smartRefreshLayout.finishLoadMoreWithNoMoreData();
        }
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        loadMoreData();
    }
}
